package com.inspectortime.webapp.servlet;

import java.io.IOException;

import javax.persistence.Transient;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.repository.PersistentEntityServiceAggregator;

public class LogoImageViewServlet extends HttpServlet {
	
	private static PersistentEntityServiceAggregator services;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String path = request.getPathInfo();
		int dotPos = path.lastIndexOf(".");
		String inspectorCompanyShortName = path.substring(1, dotPos);
		//String inspectorCompanyShortName = RequestPathUtils.extractInspectorCompanyShortNameFromPath(request);
		InspectorCompany inspectorCompany = services.getRepository().findInspectorCompanyByShortName(inspectorCompanyShortName);
		if (inspectorCompany == null) {
			return;
		}
		inspectorCompany.writeLogoImageTo(response.getOutputStream());
		response.getOutputStream().close();		
	}

	public void destroy() {
	}
	
	@Transient
	public static void setServices(PersistentEntityServiceAggregator s) {
		services = s;
	}
}
